#include "ThemeScene.h"
#include "utils.h"
#include "log.h"
#include "PTScene.h"
#include "GameData.h"
#include "ThemeSprite.h"
#include "SceneManager.h"

USING_NS_STD;

extern GameData * m_gGameData;

string ThemeScene::title(){
	return "Select Theme";
}

bool ThemeScene::init(){
	if(!CCLayer::init())
		return false;

	m_gGameData = GameData::getInstance();

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCLayer * itemContainer = CCLayer::create();
	//add children to container
	int count = 0;
	CCArray * array = 0;
	if(m_themeId == kTheme)
		count = m_gGameData->getThemeItemCount();
	else{
		ThemeItem * item = m_gGameData->getThemeItem(m_themeSceneId);

		array = item->getThemeScenes();

		if(array)
		{
			count = array->count();
			CCLog("NULL,scene count =  %d",count);
		}
	}

	int width = 0;
	for(int i = 0 ;i < count;i++){
		ThemeSprite * itemSprite = ThemeSprite::create();
		if(itemSprite){
			if (m_themeId == kTheme) {
				itemSprite->initWithFile(
						m_gGameData->getThemeItem(i)->getThumbPath());
				itemSprite->setName(m_gGameData->getThemeItem(i)->getName());
				itemSprite->setThumbPath(m_gGameData->getThemeItem(i)->getThumbPath());

			}else{
				ThemeItemScene * themeItemScene = (ThemeItemScene *)(array->objectAtIndex(i));
				itemSprite->initWithFile(themeItemScene->getThumbPath());
				itemSprite->setName(themeItemScene->getName());
				itemSprite->setThumbPath(themeItemScene->getThumbPath());
			}

			CCSize tmpSize = itemSprite->getContentSize();
			width = tmpSize.width;
			itemSprite->setId(i);
			itemSprite->setCategory(m_themeId);
			itemSprite->setAnchorPoint(ccp(0,0.5));
			itemSprite->setPosition(ccp(i * (width + 50) + 50,size.height/2));
			itemContainer->addChild(itemSprite);
		}
	}
	itemContainer->setContentSize(CCSizeMake(count*(width+50), size.height));
	itemContainer->setAnchorPoint(ccp(0,0));
	itemContainer->setPosition(ccp(0,0));

	m_pScrollView = CCScrollView::create();
	m_pScrollView->setDelegate(this);
	m_pScrollView->setContentOffset(CCPointZero);
	m_pScrollView->setContainer(itemContainer);
	m_pScrollView->setViewSize(CCSizeMake(size.width, size.height));
	m_pScrollView->setContentSize(CCSizeMake(count*(width+50), size.height));
	m_pScrollView->setDirection(kCCScrollViewDirectionHorizontal);
	m_pScrollView->setPosition(ccp(0,0));

	addChild(m_pScrollView,1);

	//add bg
	CCSprite * bgSprite = CCSprite::create("scroll_bg.png");
	bgSprite->setPosition(ccp(size.width/2,size.height/2));
	addChild(bgSprite,0);

	//add title
	CCSprite *titleSprite = CCSprite::create("select_theme.png");
	titleSprite->setPosition(ccp(size.width/2,size.height-20));
	addChild(titleSprite,2);

	return true;
}

void ThemeScene::onBegin(CCObject * pSender){

}

void ThemeScene::keyBackClicked(){
	CCDirector * director = CCDirector::sharedDirector();
	director->popScene();

}

ThemeScene * ThemeScene::create(int themeId,int themeSceneId){
	ThemeScene * scene = new ThemeScene();
	scene->m_themeId = themeId;
	scene->m_themeSceneId = themeSceneId;
	scene->init();
	scene->autorelease();
	return scene;
}

CCScene* ThemeScene::scene(int themeId,int themeSceneId){
	CCScene * scene = CCScene::create();
	ThemeScene * layer = ThemeScene::create(themeId,themeSceneId);
	scene->addChild(layer);
	return scene;
}

void ThemeScene::onEnter()
{
	CCLayer::onEnter();
}

void ThemeScene::onExit(){
	CCLayer::onExit();
}

void ThemeScene::onExitTransitionDidStart(){
	CCLayer::onExitTransitionDidStart();
	//CC_SAFE_RELEASE_NULL(m_pScrollView);
}

void ThemeScene::onLoad(CCObject *pSender){

}

void ThemeScene::scrollViewDidScroll(CCScrollView *view){
	LOG_FUNCTION_NAME;
}

void ThemeScene::scrollViewDidZoom(CCScrollView *view){

}
